//
//  AppDelegate.m
//  ShareYOU
//
//  Created by Suns孙泉 on 14/12/17.
//  Copyright (c) 2014年 ShareYOU. All rights reserved.
//

#import "AppDelegate.h"
#import "RecommendViewController.h"
#import "MainViewController.h"
#import "ShareViewController.h"
#import "SelectTrainViewController.h"


//
#import "WeiboSDK.h"
#define kAppKey         @"2045436852"
#define kRedirectURI    @"http://www.sina.com"

////引用第三方需要加的头文件
@interface WBBaseRequest ()
- (void)debugPrint;
@end

@interface WBBaseResponse ()
- (void)debugPrint;
@end



@interface AppDelegate ()

@end

@implementation AppDelegate
////
@synthesize wbtoken;
@synthesize wbCurrentUserID;




- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [_window release];
    
    UITabBarController *tabBarVC = [[UITabBarController alloc] init];
    
    RecommendViewController *recommemdVC = [[RecommendViewController alloc] init];
    UINavigationController *recommendNVC = [[UINavigationController alloc] initWithRootViewController:recommemdVC];
    UIImage *imageIconScenery = [UIImage imageNamed:@"recommend.png"];
    imageIconScenery = [imageIconScenery imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    recommendNVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"景" image:imageIconScenery tag:1] autorelease];
    
    ShareViewController *shareVC = [[ShareViewController alloc] init];
    UINavigationController *shareNVC = [[UINavigationController alloc] initWithRootViewController:shareVC];
    UIImage *imageIconShare = [UIImage imageNamed:@"share.png"];
    imageIconShare = [imageIconShare imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    shareNVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"晒" image:imageIconShare tag:2];
    
    MainViewController *acceptVC = [[MainViewController alloc] init];
    UINavigationController *acceptNVC = [[UINavigationController alloc] initWithRootViewController:acceptVC];
    UIImage *imageIconAccount = [UIImage imageNamed:@"account.png"];
    imageIconAccount = [imageIconAccount imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    acceptNVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"记" image:imageIconAccount tag:3] autorelease];
    
    SelectTrainViewController *selectTrainVC = [[SelectTrainViewController alloc] init];
    UINavigationController *selectTrainNVC = [[UINavigationController alloc] initWithRootViewController:selectTrainVC];
    UIImage *imageIconBuy = [UIImage imageNamed:@"buy.png"];
    imageIconBuy = [imageIconBuy imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    selectTrainNVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"购" image:imageIconBuy tag:4];
    
    tabBarVC.viewControllers = [NSArray arrayWithObjects:recommendNVC, shareNVC, acceptNVC, selectTrainNVC, nil];

    
    [recommendNVC release];
    [shareNVC release];
    [acceptNVC release];
    [selectTrainNVC release];
    
    [recommemdVC release];
    [shareVC release];
    [acceptVC release];
    [selectTrainVC release];
    
    self.window.rootViewController = tabBarVC;
    [tabBarVC release];
    
//    //分享需要加的第三方类的引用
//    [ShareSDK registerApp:@"api20"];
//    
//    //添加新浪微博应用 注册网址 http://open.weibo.com
//    [ShareSDK connectSinaWeiboWithAppKey:@"2868980231"
//                               appSecret:@"ebab050905bc485fedd184b476728c5f"
//                             redirectUri:@"http://www.sharesdk.cn"];
//    //当使用新浪微博客户端分享的时候需要按照下面的方法来初始化新浪的平台
//    [ShareSDK  connectSinaWeiboWithAppKey:@"2868980231"
//                                appSecret:@"ebab050905bc485fedd184b476728c5f"
//                              redirectUri:@"http://www.sharesdk.cn"
//                              weiboSDKCls:[WeiboSDK class]];
//    
//    //连接拷贝
//    [ShareSDK connectCopy];
//    
//    //激活SSO
//    [ShareSDK ssoEnabled:YES];
    //
    [WeiboSDK enableDebugMode:YES];
    [WeiboSDK registerApp:kAppKey];
    
    
    return YES;
}

//第三方分享引用
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [WeiboSDK handleOpenURL:url delegate:self];
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return [WeiboSDK handleOpenURL:url delegate:self ];
}


- (void)didReceiveWeiboRequest:(WBBaseRequest *)request
{
    
}

- (void)didReceiveWeiboResponse:(WBBaseResponse *)response
{
    if ([response isKindOfClass:WBSendMessageToWeiboResponse.class])
    {
        NSString *title = NSLocalizedString(@"发送结果", nil);
        NSString *message = [NSString stringWithFormat:@"%@: %d\n%@: %@\n%@: %@", NSLocalizedString(@"响应状态", nil), (int)response.statusCode, NSLocalizedString(@"响应UserInfo数据", nil), response.userInfo, NSLocalizedString(@"原请求UserInfo数据", nil),response.requestUserInfo];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                        message:@""
                                                       delegate:nil
                                              cancelButtonTitle:NSLocalizedString(@"确定", nil)
                                              otherButtonTitles:nil];
        WBSendMessageToWeiboResponse* sendMessageToWeiboResponse = (WBSendMessageToWeiboResponse*)response;
        NSString* accessToken = [sendMessageToWeiboResponse.authResponse accessToken];
        if (accessToken)
        {
            self.wbtoken = accessToken;
        }
        NSString* userID = [sendMessageToWeiboResponse.authResponse userID];
        if (userID) {
            self.wbCurrentUserID = userID;
        }
        
        
        [alert show];
        [alert release];
    }
    else if ([response isKindOfClass:WBAuthorizeResponse.class])
    {
        NSString *title = NSLocalizedString(@"认证结果", nil);
        NSString *message = [NSString stringWithFormat:@"%@: %d\nresponse.userId: %@\nresponse.accessToken: %@\n%@: %@\n%@: %@", NSLocalizedString(@"响应状态", nil), (int)response.statusCode,[(WBAuthorizeResponse *)response userID], [(WBAuthorizeResponse *)response accessToken],  NSLocalizedString(@"响应UserInfo数据", nil), response.userInfo, NSLocalizedString(@"原请求UserInfo数据", nil), response.requestUserInfo];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                        message:message
                                                       delegate:nil
                                              cancelButtonTitle:NSLocalizedString(@"确定", nil)
                                              otherButtonTitles:nil];
        
        self.wbtoken = [(WBAuthorizeResponse *)response accessToken];
        self.wbCurrentUserID = [(WBAuthorizeResponse *)response userID];
        [alert show];
        [alert release];
    }
    else if ([response isKindOfClass:WBPaymentResponse.class])
    {
        NSString *title = NSLocalizedString(@"支付结果", nil);
        NSString *message = [NSString stringWithFormat:@"%@: %d\nresponse.payStatusCode: %@\nresponse.payStatusMessage: %@\n%@: %@\n%@: %@", NSLocalizedString(@"响应状态", nil), (int)response.statusCode,[(WBPaymentResponse *)response payStatusCode], [(WBPaymentResponse *)response payStatusMessage], NSLocalizedString(@"响应UserInfo数据", nil),response.userInfo, NSLocalizedString(@"原请求UserInfo数据", nil), response.requestUserInfo];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                        message:message
                                                       delegate:nil
                                              cancelButtonTitle:NSLocalizedString(@"确定", nil)
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}



- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
